package com.system.service.impl;

import com.system.mapper.VendorItemAndMapper;
import com.system.pojo.Item;
import com.system.pojo.Vendor;
import com.system.service.RelationVendorItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("RelationVendorItemService")
public class RelationVendorItemServiceImpl implements RelationVendorItemService {

    @Autowired
    VendorItemAndMapper vendorItemAndMapper;

    @Override
    public List<Vendor> selectVendor(Vendor vendor) {
        return vendorItemAndMapper.selectVendor();
    }

    @Override
    public List<Vendor> selectVendorByPage(Vendor vendor, int page, int limit) {
        int starRow = (page - 1) * limit + 1;
        int endRow = page * limit;
        String vendorCode = null;
        if (vendor.getVendorCode() != null && vendor.getVendorCode() != "") {
            vendorCode = "%" + vendor.getVendorCode() + "%";
        } else {
            vendorCode = vendor.getVendorCode();
        }
        return vendorItemAndMapper.selectVendorByPage(vendorCode, starRow, endRow);
    }

    @Override
    public List<Item> selectItem(Vendor vendor) {
        return vendorItemAndMapper.selectItem(vendor);
    }

    @Override
    public List<Item> selectItemByVendor(Vendor vendor, int page, int limit) {
        int starRow = (page - 1) * limit + 1;
        int endRow = page * limit;
        Integer vendorId = vendor.getVendorId();
        return vendorItemAndMapper.selectItemByVendor(vendorId, starRow, endRow);
    }

    @Override
    public void delItemForVendor(String vendorId, String itemId) {
        try {
            vendorItemAndMapper.delItemForVendor(vendorId, itemId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Item> selectUnSelectedItem(Vendor vendor) {
        return vendorItemAndMapper.selectUnSelectedItem(vendor);
    }

    @Override
    public List<Item> selectUnSelectedItemByPage(Vendor vendor, int page, int limit) {
        int starRow = (page - 1) * limit + 1;
        int endRow = page * limit;
        Integer vendorId = vendor.getVendorId();
        return vendorItemAndMapper.selectUnSelectedItemByPage(vendorId, starRow, endRow);
    }

    @Override
    public void addItem(String vendorId, String[] idList) {
        try {
            for (String itemId : idList) {
                vendorItemAndMapper.addItem(vendorId, itemId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
